$(document).ready(function(){
	
	//Hide the Button Picklist
	$(".btn-picklist-so").hide();
	
	// Show/Hide picklist and check all checkbox for order product when you click Order checkbox
	$("input[name='so-id[]']").change(function(){
		if(this.checked){
			$(".btn-picklist-so").show();
			$("input[name='order_product[" + this.value + "][]']").prop('checked', true);
		} else {
			$(".btn-picklist-so").hide();
			$("input[name='order_product[" + this.value + "][]']").prop('checked', false);
		}

		});
	
	// Detect if atleast one checkbox is check and show Receive
	$(".so_product").change(function (e) {
		var parent_so = $(this).parents('.list-row');
		var parent_cbx = parent_so.find('.list_so_id');
		var child_cbx = parent_so.find('.so_product').length;
		var child_cbx_checked =  parent_so.find('.so_product:checked').length;

		if ( child_cbx == child_cbx_checked ) 
			parent_cbx.prop('checked', true)
		 else 
			parent_cbx.prop('checked', false)
				
		if( child_cbx_checked > 0 )
			$(".btn-picklist-so").show();
		else
			$(".btn-picklist-so").hide();
		
		//console.log( $('#form_po_list').serialize() );
			
	});
	
	
	
	window.selected_items = window.selected_items || [];
	window.filters = window.filters || {};
	window.parent_selected_items = window.parent_selected_items || [];
	
	// Initial load, check selected
	for ( var i = 0; i < selected_items.length; i++ ) 
		$('input[value=' + selected_items[i] + ']').prop('checked', true);
	
	for ( var i = 0; i < parent_selected_items.length; i++ ) 
		$('input[value=' + parent_selected_items[i] + ']').prop('checked', true);
	
	//when filter is click it reset the selected_items
	$('#filter').on('click', function(e) {
			selected_items = [];
			parent_selected_items = [];
	});
	$('#picklist_so').on('click', function(e) {
		selected_items = [];
		parent_selected_items = [];
	});
	
	
	// Expand Accordion when atleast one order product is check
	if( selected_items.length ){
		$('.list-row').each(function(index, object) {
			
			if(Number($(object).find('.so_product:checked').size())) {
				$(object).find('.panel-collapse').removeClass("panel-collapse collapse").addClass("panel-collapse collapse in");
				//$(object).find('.panel-collapse').eq(index).removeClass("panel-collapse collapse").addClass("panel-collapse collapse in");	
			}
		});
	}
	// Check all checkbox when click it
	$('.list_so_id').click(function(){
		$(this).parents('.list-row').find('input[name*=\'order_product\']').change();
	});
	
	
	// Add select/deselect event in item checkbox
	$('input[name*=\'order_product\']').on('change', function(e) {
		var order_product_id = e.target.value;
		toggleItem(order_product_id, $('input[value=' + order_product_id + ']'));
		
		//This is for parent check box
		var parent_so = $(this).parents('.list-row');
		var child_cbx = parent_so.find('.so_product').length;
		var child_cbx_checked =  parent_so.find('.so_product:checked').length;
			
		var parent_item_exists = false;
		var parent_item = $(this).parents('.list-row').find('.list_so_id');
		var parent_item_selected = parent_item.is(':checked');
		if( parent_selected_items.length ){
			$.each(parent_selected_items, function(index, value) { 
				if ( parent_item.val() == (value).toString() ) {
					parent_item_exists = true;
				}
			});
		}
		
		// SELECT FUNCTION AND EXISTED
		// Item exists and still selected? Do nothing.
		if(parent_item_exists && parent_item_selected ) return;
		
		// DESELECT FUNCTION AND EXISTED
		// Item exists in list and deselected? remove in list.
		if(parent_item_exists && !parent_item_selected) {
			parent_selected_items.splice( parent_selected_items.indexOf(parent_item.val()), 1);
			return;
		}
		
		// Remove parent checkbox checked when  Parent checkbox Item exists and child checkbox is not all checked
		if(parent_item_exists && child_cbx != child_cbx_checked ) {
			parent_selected_items.splice( parent_selected_items.indexOf(parent_item.val()), 1);
			return;
		}
	
		parent_selected_items.push(parent_item.val());
	

	
	
	});
	
	
	function toggleItem( id , item ) {

		var item_selected = item.is(':checked');
		var item_id = (id).toString();
		var item_exists = false;
	
		// Note: Workaround for InArray
		if ( selected_items.length ) {
			$.each(selected_items, function(index, value) { 
				if ( (id).toString() == (value).toString() ) {
					item_id = (value).toString();
					item_exists = true;
				}
			});
		}

		// SELECT FUNCTION AND EXISTED
		// Item exists and still selected? Do nothing.
		if ( item_exists && item_selected ) return;

		// DESELECT FUNCTION AND EXISTED
		// Item exists in list and deselected? remove in list.
		if ( item_exists && !item_selected ) {
			// Remove in Main Item list.
			selected_items.splice( selected_items.indexOf(item_id), 1);
			return;
		}

		// Add new Item in Main List
		selected_items.push(item_id);
	}
	

	$("#picklist_so").click(function(){
		$.ajax({
			url :$('base').attr('href') + '/for-delivery/generatePicklist',
			type: 'get',
			data: { 'order_product_ids': selected_items.join() } ,
			success: function(response){
				if(response){
				//alert(response);
				//self.location=$('base').attr('href')+'/po';
					//alert($('base').attr('href') + '/for-delivery/generatePicklist');
					$('#list-container').showList(document.URL);
				}
				else
					alert('not ok');
				
			}
			
			
		});
	});
	
	
});